home *** CD-ROM | disk | FTP | other *** search
- /*********************************************************************
-
- events.c
-
- This file contains the event handling code for the QuickDraw GX
- unaware sample, "Simple Sample."
-
- Additional info can be found in the related develop #19 article,
- "Adding QuickDraw GX Printing to QuickDraw Applications."
-
- Dave Hersey, Apple Developer Technical Support.
-
- ——————— Edit Trail ———————
-
- begat: 1/22/94 - dmh
- cleaned up for 2nd draft of develop article: 3/10/94 - dmh
- cleaned up for final: 4/14/94 - dmh
- added NewAEEventHandlerProc to build ppc upp 8/16/94 - nick
-
- *********************************************************************/
-
- #include "Simple Sample.h"
-
-
- /************************************************************
- MyEventLoop - This routine (in conjunction with main)
- makes up our event loop.
-
- *************************************************************/
-
- void MyEventLoop()
- {
- EventRecord theEvent;
-
- if (WaitNextEvent(everyEvent, &theEvent, gSleep, nil))
- MyDoEvent(&theEvent);
- }
-
-
- /************************************************************
- MyDoEvent - This routine handles event dispatching.
-
- *************************************************************/
-
- void MyDoEvent(EventRecord *theEvent)
- {
- char key;
- WindowPtr whichWindow;
- MyDocumentPtr windowDoc;
-
- switch(theEvent->what)
- {
- case updateEvt: // Updating a window.
-
- if (((WindowPeek) theEvent->message)->windowKind == userKind)
- MyUpdateWindow((WindowPtr) theEvent->message);
-
- break;
-
- case mouseDown: // Mousedown in a window or menu.
-
- switch (FindWindow(theEvent->where, &whichWindow))
- {
- case inSysWindow:
-
- SystemClick(theEvent, whichWindow);
- break;
-
- case inDrag:
-
- if (((WindowPeek) whichWindow)->windowKind == userKind)
- {
- DragWindow(whichWindow, theEvent->where, &qd.screenBits.bounds);
- MyAdjustMenus();
- }
- break;
-
- case inGoAway:
-
- if ((((WindowPeek) whichWindow)->windowKind == userKind) &&
- (TrackGoAway(whichWindow, theEvent->where)))
- {
- MyDisposeDocument(MyGetDocPtr(whichWindow));
- MyAdjustMenus();
- }
-
- break;
-
- case inContent:
-
- if (((WindowPeek) whichWindow)->windowKind == userKind)
- {
- SelectWindow(whichWindow);
- MyAdjustMenus();
- }
-
- break;
-
- case inMenuBar:
- MyDoMenuCommand(MenuSelect(theEvent->where));
- MyAdjustMenus();
- break;
- }
-
- case keyDown: // Key pressed.
- case autoKey:
-
- key = theEvent->message & charCodeMask;
- if ((theEvent->modifiers & cmdKey) &&
- (theEvent->what == keyDown))
- {
- MyDoMenuCommand(MenuKey(key));
- MyAdjustMenus();
- }
-
- break;
-
- case kOSEvent: // Operating system event.
-
- switch ((unsigned long) theEvent->message >> 24)
- {
- case kSuspendResumeMessage: // Suspend event:
- if ((theEvent->message & kResumeMask) == 0)
- gSleep = 80;
- else // Resume event:
- {
- gSleep = 0;
- SetCursor(&qd.arrow);
- }
-
-
- break;
- }
-
- break;
-
- case kHighLevelEvent: // AppleEvent.
-
- AEProcessAppleEvent(theEvent);
- break;
- }
- }
-
-
- /************************************************************
- MyDoAEInstallation - This routine installs our core
- AppleEvent handlers.
-
- *************************************************************/
-
- void MyDoAEInstallation()
- {
- AEInstallEventHandler(kCoreEventClass,
- kAEOpenApplication,
- NewAEEventHandlerProc(MyHandleOAPP),
- 0, false);
-
- AEInstallEventHandler(kCoreEventClass,
- kAEQuitApplication,
- NewAEEventHandlerProc(MyHandleQUIT),
- 0, false);
-
- AEInstallEventHandler(kCoreEventClass,
- kAEOpenDocuments,
- NewAEEventHandlerProc(MyHandleODOC),
- 0, false);
-
- AEInstallEventHandler(kCoreEventClass,
- kAEPrintDocuments,
- NewAEEventHandlerProc(MyHandlePDOC),
- 0, false);
- }
-
-
- /************************************************************
- MyHandleOAPP - This is the "open application" AppleEvent
- handler. We check out the message, and set our "should we
- quit after handling a 'pdoc'" flag to false. If we got an
- 'oapp', we weren't launched via 'pdoc'.
-
- *************************************************************/
-
- pascal OSErr MyHandleOAPP(AppleEvent *theAppleEvent, AppleEvent *reply, long myRefCon)
- {
- #pragma unused (reply, myRefCon)
-
- gQuitAfterPrinting = false;
- return MyCheckAEParams(theAppleEvent);
- }
-
-
- /************************************************************
- MyHandleQUIT - This is the "quit" AppleEvent handler. Set
- the gQuitting flag and the event loop will do the rest.
-
- *************************************************************/
-
- pascal OSErr MyHandleQUIT(AppleEvent *theAppleEvent, AppleEvent *reply, long myRefCon)
- {
- #pragma unused (reply, myRefCon)
-
- gQuitting = true;
- return MyCheckAEParams(theAppleEvent);
- }
-
-
- /************************************************************
- MyHandleODOC - This is the "open document" AppleEvent
- handler, so open some documents.
-
- *************************************************************/
-
- pascal OSErr MyHandleODOC(AppleEvent *theAppleEvent, AppleEvent *reply, long myRefCon)
- {
- OSErr err;
- AEDescList docList;
- FSSpec myFSS;
- AEKeyword theKeyword;
- DescType typeCode;
- long doc, itemsInList;
- Size actualSize;
- MyDocumentPtr newDocument;
-
- #pragma unused (reply, myRefCon)
-
- gQuitAfterPrinting = false;
-
- // Get the event's document list.
-
- err = AEGetParamDesc(theAppleEvent, keyDirectObject, typeAEList, &docList);
- nrequire(err, AEError);
-
- err = MyCheckAEParams(theAppleEvent);
- nrequire(err, CheckAEParamsFailed);
-
-
- // Open all entries in the document list.
-
- err = AECountItems(&docList, &itemsInList);
- nrequire(err, AECountItemsFailed);
-
- for (doc = 1; (!err) && (doc <= itemsInList); doc++)
- {
- err = AEGetNthPtr(&docList, doc, typeFSS, &theKeyword, &typeCode,
- (Ptr) &myFSS, sizeof(FSSpec), &actualSize);
-
- nrequire(err, AEEntryError);
-
- err = MyCreateDocument(kDefaultTitle, &newDocument);
- nrequire(err, CreateDocFailed);
-
- err = MyFSLoadDocument(newDocument, &myFSS, false);
-
- if (err == noErr)
- {
- ShowWindow(newDocument->documentWindow);
- SelectWindow(newDocument->documentWindow);
- }
- else
- MyDisposeDocument(newDocument);
- }
-
- CreateDocFailed:
- AEEntryError:
- AECountItemsFailed:
- CheckAEParamsFailed:
- AEDisposeDesc(&docList);
- MyAdjustMenus();
-
- AEError:
- return err;
- }
-
-
- /************************************************************
- MyHandlePDOC - This is the "print document" AppleEvent
- handler, so print some documents.
-
- *************************************************************/
-
- pascal OSErr MyHandlePDOC(AppleEvent *theAppleEvent, AppleEvent *reply, long myRefCon)
- {
- OSErr err;
- AEDescList docList;
- FSSpec myFSS;
- long itemsInList, i;
- AEKeyword theKeyword;
- DescType typeCode;
- Size actualSize;
- MyDocumentPtr newDocument;
-
- #pragma unused (reply, myRefCon)
-
- // Get our document list.
-
- err = AEGetParamDesc(theAppleEvent, keyDirectObject, typeAEList, &docList);
- nrequire(err, AEError);
-
- /*
- Make sure we've accounted for all of the parameters
- passed, and count the number of documents passed in.
- */
-
- err = MyCheckAEParams(theAppleEvent);
- nrequire(err, AEError);
-
- err = AECountItems(&docList, &itemsInList);
- nrequire(err, AEError);
-
- /*
- For each entry in the document list, load it,
- print it, and close it.
- */
-
- for (i = 1; i<= itemsInList; i++)
- {
- err = AEGetNthPtr(&docList, i, typeFSS, &theKeyword, &typeCode,
- (Ptr) &myFSS, sizeof(FSSpec), &actualSize);
-
- nrequire(err, AEEntryError);
-
- // Load the document.
-
- err = MyCreateDocument(kDefaultTitle, &newDocument);
- nrequire(err, CreateDocFailed);
-
- err = MyFSLoadDocument(newDocument, &myFSS, true);
- nrequire(err, LoadDocFailed);
-
- // Print the document.
-
- err = MyPrintDocument(newDocument);
-
- // Close the document once it's printed.
-
- LoadDocFailed:
- MyDisposeDocument(newDocument);
- }
-
- // When we're all done throw away the description lists and return.
-
- CreateDocFailed:
- AEEntryError:
- AEDisposeDesc(&docList);
-
- AEError:
- if (gQuitAfterPrinting)
- gQuitting = true;
-
- return err;
- }
-
-
- /************************************************************
- MyCheckAEParams - This routine is used to make sure we've
- received all the parameters for an AppleEvent. If so, we
- return noErr, otherwise we indicate that the event wasn't
- handled completely.
-
- *************************************************************/
-
- OSErr MyCheckAEParams(AppleEvent *theAppleEvent)
- {
- OSErr err;
- DescType typeCode;
- Size actualSize;
-
- err = AEGetAttributePtr(theAppleEvent, keyMissedKeywordAttr,
- typeWildCard, &typeCode, nil, 0, &actualSize);
-
- if (err == errAEDescNotFound) return noErr;
- if (err == noErr) return errAEEventNotHandled;
-
- return err;
- }
-